import pymysql
import urllib.request
import json

db = pymysql.connect("114.115.160.16", "root", "123456Aa!", "fund")
cursor = db.cursor()

headers = {
    "token": "atTPd9c8sA"
}
req = urllib.request.Request(url="https://api.doctorxiong.club/v1/fund/detail?code=", headers=headers)
response = urllib.request.urlopen(req)
all_fund = json.loads(response.read().decode("UTF-8"))
idid = 1

number_of_fund = 1000

sql = "select fund_id,category from api_fund"

cursor.execute(sql)

for data in cursor.fetchall():
    fund_id = data[0]
    fund_category = data[1]
    req = urllib.request.Request(url="https://api.doctorxiong.club/v1/fund/detail?code=" + fund_id, headers=headers)
    response = urllib.request.urlopen(req)
    fund = json.loads(response.read().decode("UTF-8"))
    category = fund["data"]["type"]
    if category == "混合型":
        if fund_category != 2:
            sql = "update api_fund set category=2 where fund_id='%s'" % (fund_id)
            cursor.execute(sql)
            db.commit()
    elif category == "股票型":
        if fund_category != 1:
            sql = "update api_fund set category=1 where fund_id='%s'" % (fund_id)
            cursor.execute(sql)
            db.commit()
    elif category == "债券型":
        if fund_category != 3:
            sql = "update api_fund set category=3 where fund_id='%s'" % (fund_id)
            cursor.execute(sql)
            db.commit()
    elif category == "指数型":
        if fund_category != 4:
            sql = "update api_fund set category=4 where fund_id='%s'" % (fund_id)
            cursor.execute(sql)
            db.commit()
    elif category == "货币型":
        if fund_category != 5:
            sql = "update api_fund set category=5 where fund_id='%s'" % (fund_id)
            cursor.execute(sql)
            db.commit()
    print("ok!!",fund_id)
'''
for fund in all_fund["data"][0:number_of_fund]:
    fund_id = fund[0]  # 基金代码
    fund_name = fund[2]  # 基金名称
    category = fund[3]  # 基金类型
    if category == "股票型":
        category = 1
    elif category == "混合型":
        category = 2
    elif category == "债券型":
        category = 3
    elif category == "指数型":
        category = 4
    else:
        category = 5

    try:
        response = urllib.request.urlopen("https://api.doctorxiong.club/v1/fund/detail" + "?code=" + fund[
            0] + "&startDate=2021-04-14" + "&token=atTPd9c8sA").read().decode("UTF-8")
        fund_info = eval(response)["data"]
        buy_min = float(fund_info["buyMin"])
        manager = fund_info["manager"]
        buy_source_rate = float(fund_info["buySourceRate"])
        buy_rate = float(fund_info["buyRate"])
        day_growth = float(fund_info["dayGrowth"])
        week_growth = float(fund_info["lastWeekGrowth"])
        month_growth = float(fund_info["lastMonthGrowth"])
        year_growth = float(fund_info["lastYearGrowth"])
        sql = "insert into api_fund values('%s','%s',%d,%f,'%s',%f,%f,%f,%f,%f,%f)" % (
        fund_id, fund_name, category, buy_min, manager, buy_source_rate, buy_rate, day_growth, week_growth,
        month_growth, year_growth)
        cursor.execute(sql)
        db.commit()
        print("ok!!", idid)
    except:
        print("fail!!", fund_info)
    idid += 1
'''